/**
 * Copyright (c) 2012, SHX Studio.
 * All rights reserved.
 * rosary226@gmail.com
 * 
 * @author shenqianfeng
 * @date 2012-9-6 下午12:49:21   
 * @version 1.0   
 */
package com.xxx.sqfengine.primitives;

import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;

import com.xxx.sqfengine.SQFGL3DObject;
import com.xxx.sqfengine.util.SQFGLBufferUtil;
import com.xxx.sqfengine.util.SQFGLConstants;

/*
                     3             2
                    /------------/
                   / |          /|
                 7/--|---------/ |
                  |  |        6| |
                  |  | 0       | | 1
                  |  /---------|-/
                  | /          |/
                4 |------------/ 5
*/
/**
 * @author shenqianfeng
 */
public class SQFGLCube extends SQFGL3DObject{
	
	private FloatBuffer mVertexBuffer;
	private ShortBuffer mIndicesBuffer;
	private FloatBuffer mColorBuffer;
	
	private static short indices[] = { 0, 4, 5,
             0, 5, 1,
             1, 5, 6,
             1, 6, 2,
             2, 6, 7,
             2, 7, 3,
             3, 7, 4,
             3, 4, 0,
             4, 7, 6,
             4, 6, 5,
             3, 0, 1,
             3, 1, 2, };
	
	public SQFGLCube(SQFGLVertex backLeftBottom, SQFGLVertex backRightBottom,
					 SQFGLVertex backRightTop, SQFGLVertex backLeftTop,
					 SQFGLVertex frontLeftBottom, SQFGLVertex frontRightBottom,
					 SQFGLVertex frontRightTop, SQFGLVertex frontLeftTop) {
		mColorBuffer = buildColorBufferFrom8Vertices(backLeftBottom, backRightBottom, backRightTop, backLeftTop,
				 										frontLeftBottom, frontRightBottom, frontRightTop, frontLeftTop);
		mVertexBuffer = buildVertexBufferFrom8Vertices(backLeftBottom, backRightBottom, backRightTop, backLeftTop, 
														frontLeftBottom, frontRightBottom, frontRightTop, frontLeftTop);
		mIndicesBuffer = SQFGLBufferUtil.toShortBuf(indices);
	}
	
	public SQFGLCube(float width, float height, float depth) {
		width  /= 2;
        height /= 2;
        depth  /= 2;
        
 
        float vertices[] = { -width, -height, -1*depth - 0.5f, // 0 backLeftBottom
                              width, -height, -1*depth - 0.5f, // 1 backRightBottom
                              width,  height, -1*depth - 0.5f, // 2 backRightTop
                             -width,  height, -1*depth - 0.5f, // 3 backLeftTop
                             -width, -height,  depth - 0.5f, // 4 frontLeftBottom
                              width, -height,  depth - 0.5f, // 5 frontRightBottom
                              width,  height,  depth - 0.5f, // 6 frontRightTop
                             -width,  height,  depth - 0.5f, // 7 frontLeftTop
        };
 
/*        short indices[] = { 0, 4, 5,
                            0, 5, 1, //bottom
                            1, 5, 6,
                            1, 6, 2,
                            2, 6, 7,
                            2, 7, 3,
                            3, 7, 4,
                            3, 4, 0,
                            4, 7, 6,
                            4, 6, 5,
                            3, 0, 1,
                            3, 1, 2, };*/
//    	short indices[] = { 
//    				7, 4, 5,
//    				7, 5, 6,
//    				3, 2, 1,
//    				3, 1 ,0,
//    				6, 5, 1,
//    				6, 1, 2,
//    				3, 7, 4,
//    				3, 4, 0,
//    				3, 7, 6, 
//    				3, 6, 2,
//    				0, 1, 5, 
//    				0, 5, 4
//    			};

        
        mVertexBuffer = SQFGLBufferUtil.toFloatBuf(vertices);
        mIndicesBuffer = SQFGLBufferUtil.toShortBuf(indices);
        
	}
	
	public FloatBuffer buildVertexBufferFrom8Vertices(SQFGLVertex backLeftBottom, SQFGLVertex backRightBottom,
			 SQFGLVertex backRightTop, SQFGLVertex backLeftTop,
			 SQFGLVertex frontLeftBottom, SQFGLVertex frontRightBottom,
			 SQFGLVertex frontRightTop, SQFGLVertex frontLeftTop) {
		float [] buf = new float[24];
		FloatBuffer fb = FloatBuffer.wrap(buf);
		fb.position(0);
		fb.put(backLeftBottom.toVerticesFloatBuf());
		fb.put(backRightBottom.toVerticesFloatBuf());
		fb.put(backRightTop.toVerticesFloatBuf());
		fb.put(backLeftTop.toVerticesFloatBuf());
		fb.put(frontLeftBottom.toVerticesFloatBuf());
		fb.put(frontRightBottom.toVerticesFloatBuf());
		fb.put(frontRightTop.toVerticesFloatBuf());
		fb.put(frontLeftTop.toVerticesFloatBuf());
		fb.limit(24);
		return fb;
	}

	
	public FloatBuffer buildColorBufferFrom8Vertices(SQFGLVertex backLeftBottom, SQFGLVertex backRightBottom,
			 SQFGLVertex backRightTop, SQFGLVertex backLeftTop,
			 SQFGLVertex frontLeftBottom, SQFGLVertex frontRightBottom,
			 SQFGLVertex frontRightTop, SQFGLVertex frontLeftTop) {
		float [] buf = new float[32];
		FloatBuffer fb = FloatBuffer.wrap(buf);
		fb.position(0);
		fb.put(backLeftBottom.getColor().toFloatBuffer());
		fb.put(backRightBottom.getColor().toFloatBuffer());
		fb.put(backRightTop.getColor().toFloatBuffer());
		fb.put(backLeftTop.getColor().toFloatBuffer());
		fb.put(frontLeftBottom.getColor().toFloatBuffer());
		fb.put(frontRightBottom.getColor().toFloatBuffer());
		fb.put(frontRightTop.getColor().toFloatBuffer());
		fb.put(frontLeftTop.getColor().toFloatBuffer());
		fb.limit(32);
		return fb;
	}
	/**
	 * @author shenqianfeng
	 * @param gl
	 * @see com.xxx.sqfengine.SQFGL3DObject#draw(javax.microedition.khronos.opengles.GL10)
	 */
	@Override
	public void draw(GL10 gl) {
		//GLU.gluLookAt(gl, 0.0f, 0.0f, 5.0f, 0.0f, 0.0f, -10.0f, 0.0f, 1.0f, 5.0f);
		beginDraw(gl);
		
		
		getTransformManager().draw(gl);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
		gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
		gl.glDrawElements(GL10.GL_TRIANGLES, 36, GL10.GL_UNSIGNED_SHORT, mIndicesBuffer);
		//gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 36);

		endDraw(gl);
	}
}
